package com.hunttown.mes.impl;

import com.hunttown.mes.rpc.domain.TransportExpenseDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportExpenseRpcService;
import com.hunttown.mes.rpc.domain.query.TransportExpenseDTOQuery;
import com.hunttown.mes.service.TransportExpenseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportExpenseConverts.convert;

/**
 * created by wangjunfu on 2019-07-07 10:50:27
 */
@Service
public class TransportExpenseRpcServiceImpl implements TransportExpenseRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportExpenseRpcServiceImpl.class);

    private final TransportExpenseService transportExpenseService;

    @Autowired
    public TransportExpenseRpcServiceImpl(TransportExpenseService transportExpenseService) {
        this.transportExpenseService = transportExpenseService;
    }

    @Override
    public TransportExpenseDTO insert(TransportExpenseDTO transportExpenseDTO) {
        try {
            return convert(transportExpenseService.insert(convert(transportExpenseDTO)));
        } catch (Exception e) {
            logger.error("TransportExpense create failed.", e);
            throw new RuntimeException("TransportExpense create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportExpenseDTO transportExpenseDTO) {
        try {
            return transportExpenseService.updateInfoById(convert(transportExpenseDTO));
        } catch (Exception e) {
            logger.error("TransportExpense update by id failed.", e);
            throw new RuntimeException("TransportExpense update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportExpenseDTOQuery transportExpenseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportExpenseDTOQuery.getW_id() != null && transportExpenseDTOQuery.getW_id() > 0) {
                query.put("w_id", transportExpenseDTOQuery.getW_id());
            }

            if (transportExpenseDTOQuery.getW_relationId() != null && transportExpenseDTOQuery.getW_relationId() > 0) {
                query.put("w_relationId", transportExpenseDTOQuery.getW_relationId());
            }

            if (transportExpenseDTOQuery.getIds() != null && !transportExpenseDTOQuery.getIds().equals("")) {
                query.put("ids", transportExpenseDTOQuery.getIds());
            }

            if (transportExpenseDTOQuery.getBatchNumber() != null) {
                query.put("batchNumber", transportExpenseDTOQuery.getBatchNumber());
            }

            if (transportExpenseDTOQuery.getReviewer() != null) {
                query.put("reviewer", transportExpenseDTOQuery.getReviewer());
            }

            if (transportExpenseDTOQuery.getReviewDate() != null) {
                query.put("reviewDate", transportExpenseDTOQuery.getReviewDate());
            }

            if (transportExpenseDTOQuery.getReviewStatus() != null) {
                query.put("reviewStatus", transportExpenseDTOQuery.getReviewStatus());
            }

            if (transportExpenseDTOQuery.getBxStatus() != null) {
                query.put("bxStatus", transportExpenseDTOQuery.getBxStatus());
            }

            if (transportExpenseDTOQuery.getRelationId() != null) {
                query.put("relationId", transportExpenseDTOQuery.getRelationId());
            }

            if (transportExpenseDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportExpenseDTOQuery.getStopFlag());
            }

            if (transportExpenseDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportExpenseDTOQuery.getDeleteFlag());
            }

            return transportExpenseService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportExpense update by query failed.", e);
            throw new RuntimeException("TransportExpense update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportExpenseDTO selectById(Integer id) {
        try {
            return convert(transportExpenseService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportExpense select by id failed.", e);
            throw new RuntimeException("TransportExpense select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportExpenseDTO selectObjByQuery(TransportExpenseDTOQuery transportExpenseDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportExpenseDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportExpenseDTOQuery.getStopFlag());
            }

            if (transportExpenseDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportExpenseDTOQuery.getDeleteFlag());
            }

            return convert(transportExpenseService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportExpense select obj by query failed.", e);
            throw new RuntimeException("TransportExpense select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportExpenseDTO> getListForPage(TransportExpenseDTOQuery expenseDTOQuery) {
        Query query = Query.page(expenseDTOQuery.getPn(), expenseDTOQuery.getPageSize());

        if (expenseDTOQuery.getId() != null && expenseDTOQuery.getId() > 0) {
            query.put("id", expenseDTOQuery.getId());
        }

        if (expenseDTOQuery.getParentId() != null) {
            query.put("parentId", expenseDTOQuery.getParentId());
        }

        if (expenseDTOQuery.getIds() != null && !expenseDTOQuery.getIds().equals("")) {
            query.put("ids", expenseDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getLikeName())) {
            query.put("likeName", expenseDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getLinkNameLike())) {
            query.put("linkNameLike", expenseDTOQuery.getLinkNameLike());
        }

        if (expenseDTOQuery.getCompanyId() != null && expenseDTOQuery.getCompanyId() != -9999) {
            query.put("companyId", expenseDTOQuery.getCompanyId());
        }

        if (expenseDTOQuery.getBxType() != null && expenseDTOQuery.getBxType() > 0) {
            query.put("bxType", expenseDTOQuery.getBxType());
        }

        if (expenseDTOQuery.getBxProject() != null && expenseDTOQuery.getBxProject() > 0) {
            query.put("bxProject", expenseDTOQuery.getBxProject());
        }

        if (expenseDTOQuery.getBxPeople() != null && expenseDTOQuery.getBxPeople() > 0) {
            query.put("bxPeople", expenseDTOQuery.getBxPeople());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getShowDate())) {
            query.put("showDate", expenseDTOQuery.getShowDate());
        }

        //不包括取消和撤消、驳回数据（弃用）
        if (expenseDTOQuery.getNoCancle() != null && expenseDTOQuery.getNoCancle() == 1) {
            query.put("noCancle", expenseDTOQuery.getNoCancle());
        }

        //报销日期
        if (StringUtils.isNotBlank(expenseDTOQuery.getDaterange()) && (expenseDTOQuery.getIsUnLimitDate() == null || expenseDTOQuery.getIsUnLimitDate().equals(0))) {
            String[] date = expenseDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getBatchNumber())) {
            query.put("batchNumber", expenseDTOQuery.getBatchNumber());
        }

        if (expenseDTOQuery.getNoBatchNumber() != null && expenseDTOQuery.getNoBatchNumber() > 0) {
            query.put("noBatchNumber", expenseDTOQuery.getNoBatchNumber());
        }

        if (expenseDTOQuery.getBxStatus() != null && expenseDTOQuery.getBxStatus() != -9999) {
            query.put("bxStatus", expenseDTOQuery.getBxStatus());
        }

        if (expenseDTOQuery.getRelationId() != null) {
            query.put("relationId", expenseDTOQuery.getRelationId());
        }

        if (expenseDTOQuery.getCurr_relation() != null) {
            query.put("currRelation", expenseDTOQuery.getCurr_relation());
        }

        if (expenseDTOQuery.getAdminId() != null) {
            query.put("adminId", expenseDTOQuery.getAdminId());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getOrderBy())) {
            query.put("orderBy", expenseDTOQuery.getOrderBy());
        }

        if (expenseDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", expenseDTOQuery.getStopFlag());
        }

        if (expenseDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", expenseDTOQuery.getDeleteFlag());
        }

        //显示取消数据
        if (expenseDTOQuery.getShowCancle() != null && expenseDTOQuery.getShowCancle() == 1) {
            query.remove("stopFlag");
        }

        //显示驳回数据
        if (expenseDTOQuery.getShowReject() != null && expenseDTOQuery.getShowReject() == 1) {
            List<Integer> list = expenseDTOQuery.getStatuList();
            list.add(-1);
            expenseDTOQuery.setStatuList(list);
        }

        //显示撤回数据
        if (expenseDTOQuery.getShowRecall() != null && expenseDTOQuery.getShowRecall() == 1) {
            List<Integer> list = expenseDTOQuery.getStatuList();
            list.add(-10);
            expenseDTOQuery.setStatuList(list);
        }

        //要查询的状态
        if (expenseDTOQuery.getStatuList() != null && expenseDTOQuery.getStatuList().size() > 0) {
            query.put("statuList", expenseDTOQuery.getStatuList());
        }

        //跟自己有关的单子
        if (expenseDTOQuery.getSecondaryCurrUser() != null) {
            query.put("secondaryCurrUser", expenseDTOQuery.getSecondaryCurrUser());
        }

        List<TransportExpenseDTO> list = convert(transportExpenseService.selectByQuery(query));
        Page<TransportExpenseDTO> page = Page.from(expenseDTOQuery.getPn(), expenseDTOQuery.getPageSize(), list);
        page.setTotalItems(transportExpenseService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportExpenseService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportExpense delete failed.", e);
            throw new RuntimeException("TransportExpense delete failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportExpenseDTO> getJoinSecondaryForPage(TransportExpenseDTOQuery expenseDTOQuery) {
        Query query = Query.page(expenseDTOQuery.getPn(), expenseDTOQuery.getPageSize());

        if (expenseDTOQuery.getId() != null && expenseDTOQuery.getId() > 0) {
            query.put("exId", expenseDTOQuery.getId());
        }

        if (expenseDTOQuery.getParentId() != null) {
            query.put("parentId", expenseDTOQuery.getParentId());
        }

        if (expenseDTOQuery.getIds() != null && !expenseDTOQuery.getIds().equals("")) {
            query.put("exIds", expenseDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getLikeName())) {
            query.put("likeName", expenseDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getLinkNameLike())) {
            query.put("linkNameLike", expenseDTOQuery.getLinkNameLike());
        }

        if (expenseDTOQuery.getCompanyId() != null && expenseDTOQuery.getCompanyId() != -9999) {
            query.put("companyId", expenseDTOQuery.getCompanyId());
        }

        if (expenseDTOQuery.getBxType() != null && expenseDTOQuery.getBxType() > 0) {
            query.put("bxType", expenseDTOQuery.getBxType());
        }

        if (expenseDTOQuery.getBxProject() != null && expenseDTOQuery.getBxProject() > 0) {
            query.put("bxProject", expenseDTOQuery.getBxProject());
        }

        if (expenseDTOQuery.getBxPeople() != null && expenseDTOQuery.getBxPeople() > 0) {
            query.put("bxPeople", expenseDTOQuery.getBxPeople());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getShowDate())) {
            query.put("showDate", expenseDTOQuery.getShowDate());
        }

        //不包括取消和撤消、驳回数据（弃用）
        if (expenseDTOQuery.getNoCancle() != null && expenseDTOQuery.getNoCancle() == 1) {
            query.put("noCancle", expenseDTOQuery.getNoCancle());
        }

        //报销日期
        if (StringUtils.isNotBlank(expenseDTOQuery.getDaterange()) && (expenseDTOQuery.getIsUnLimitDate() == null || expenseDTOQuery.getIsUnLimitDate().equals(0))) {
            String[] date = expenseDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getBatchNumber())) {
            query.put("batchNumber", expenseDTOQuery.getBatchNumber());
        }

        if (expenseDTOQuery.getNoBatchNumber() != null && expenseDTOQuery.getNoBatchNumber() > 0) {
            query.put("noBatchNumber", expenseDTOQuery.getNoBatchNumber());
        }

        if (expenseDTOQuery.getBxStatus() != null && expenseDTOQuery.getBxStatus() != -9999) {
            query.put("bxStatus", expenseDTOQuery.getBxStatus());
        }

        if (expenseDTOQuery.getRelationId() != null) {
            query.put("relationId", expenseDTOQuery.getRelationId());
        }

        if (expenseDTOQuery.getCurr_relation() != null) {
            query.put("currRelation", expenseDTOQuery.getCurr_relation());
        }

        if (expenseDTOQuery.getAdminId() != null) {
            query.put("adminId", expenseDTOQuery.getAdminId());
        }

        if (StringUtils.isNotBlank(expenseDTOQuery.getOrderBy())) {
            query.put("orderBy", expenseDTOQuery.getOrderBy());
        }

        if (expenseDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", expenseDTOQuery.getStopFlag());
        }

        if (expenseDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", expenseDTOQuery.getDeleteFlag());
        }

        //显示取消数据
        if (expenseDTOQuery.getShowCancle() != null && expenseDTOQuery.getShowCancle() == 1) {
            query.remove("stopFlag");
        }

        //显示驳回数据
        if (expenseDTOQuery.getShowReject() != null && expenseDTOQuery.getShowReject() == 1) {
            List<Integer> list = expenseDTOQuery.getStatuList();
            list.add(-1);
            expenseDTOQuery.setStatuList(list);
        }

        //显示撤回数据
        if (expenseDTOQuery.getShowRecall() != null && expenseDTOQuery.getShowRecall() == 1) {
            List<Integer> list = expenseDTOQuery.getStatuList();
            list.add(-10);
            expenseDTOQuery.setStatuList(list);
        }

        //要查询的状态
        if (expenseDTOQuery.getStatuList() != null && expenseDTOQuery.getStatuList().size() > 0) {
            query.put("statuList", expenseDTOQuery.getStatuList());
        }

        //跟自己有关的单子
        if (expenseDTOQuery.getSecondaryCurrUser() != null) {
            query.put("secondaryCurrUser", expenseDTOQuery.getSecondaryCurrUser());
        }

        List<TransportExpenseDTO> list = convert(transportExpenseService.selectJoinSecondaryForPage(query));
        Page<TransportExpenseDTO> page = Page.from(expenseDTOQuery.getPn(), expenseDTOQuery.getPageSize(), list);
        page.setTotalItems(transportExpenseService.countJoinSecondaryByQuery(query));

        return page;
    }

    @Override
    public List<Map<String, String>> statisticExpense(Integer searchYear) {
        try {
            return transportExpenseService.statisticExpense(searchYear);
        } catch (Exception e) {
            logger.error("TransportExpense statistic failed.", e);
            throw new RuntimeException("TransportExpense statistic failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> statisticExpenseByType(Integer searchYear) {
        try {
            return transportExpenseService.statisticExpenseByType(searchYear);
        } catch (Exception e) {
            logger.error("TransportExpense statistic failed.", e);
            throw new RuntimeException("TransportExpense statistic failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> statisticZHExpense(Integer searchYear) {
        try {
            return transportExpenseService.statisticZHExpense(searchYear);
        } catch (Exception e) {
            logger.error("TransportExpense statistic failed.", e);
            throw new RuntimeException("TransportExpense statistic failed, Please order your code.", e);
        }
    }
}